package org.grow.present.interceptor;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.grow.present.dto.Msg;

import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;

import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;

import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.util.Arrays;

/**
 * @Author: xwg
 * @CreateDate: 2024/10/7
 */

//@RestControllerAdvice
//    能作用在ResponseEntity上但是不能获取其status body还是ResponseEntity的body
public class ResponseStyleFormatter implements ResponseBodyAdvice {
    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
//        System.out.println(returnType.getMethod());
//        静态资源属于responseEntity
//        动态资源属于 自由类型 T
//        动态资源中的异常属于？ResponseEntity
//        特别的动态资源属于正常的Result的则不转化
//        System.out.println(returnType.getParameterType());
//        System.out.println("convert "+converterType);
//        if()
        return true;
    }

    //    @Autowired
    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        ;
        System.out.println("-------------" + request.getURI() + "---" + returnType.getMethod().getName());
        System.out.println(returnType.getMethod().getDeclaringClass().getName());
        System.out.println(returnType.getClass().getName());
        System.out.println(response);
        if (request.getURI().toString().contains("v3")) {
            return body;
        }
//        System.out.println(this.getClass().getName().);
        String name = returnType.getMethod().getDeclaringClass().getName();
        if (!name.contains("org.grow.present")) {
            return body;
        }
        if (name.contains("exception") || name.contains("Exception")) {

//            System.out.println(Arrays.asList(returnType.getMethodAnnotations()));
//            System.out.println("000000000000000000000000"+body.getClass().getName());
//            System.out.println(request.getPrincipal().getName());
//            System.out.println(request.);
//            ResponseEntity r = (ResponseEntity) body;
//            Result<Object> result = Result.builder().code(r.getStatusCode().value()).message(r.getBody().toString())
//                    .data(r.getBody()).build();
//            return result;
            return body;
        }
        if (body instanceof Msg<?>) {
            return body;
        }
        if (body instanceof String) {
//            Result<Object> success = Result.builder().code(200).message("success").data(body).build();
            Msg success = Msg.success().data(body);
            String s = "";
            try {
                s = objectMapper.writeValueAsString(success);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            return s;
        }
//        Result<Object> r = Result.builder().code(200).message("success").data(body).build();
        Msg r = Msg.success().data(body);
        return r;
    }
}
